<template>
<view>
<!-- <import src="../../view-new/partials/product_search.wxml"/> -->

<!-- <import src="../../view/partials/type_search.wxml"/> --><!-- type_search -->


<view class="container"> 
  
  <view class="typeSearchContainer1"> 
      <view class="search_input_bg">
        <view class="search_input">
          <view class="search_input_left">
            <image class="input_img" src="https://image1.sansancloud.com/xianhua/2018_9/14/16/25/0_222.jpg?x-oss-process=style/preview_120"></image>
            <!--   data-link="search_page.html"  bindtap='linkUrl'-->
            <input class="input" @confirm="getSearchProductName" @input="searchProductValue" placeholder="搜索产品名" :value="searchProductName"></input>
          </view>
          <view class="empty_button" @tap="getSearchProductName"><text>x</text></view>
        </view>
      </view>
  </view>
  
<!--   <template is="product_search" data="{{...topName,...setting}}"></template>  -->
  <!-- <template is="typeSearch" data="{{...setting.platformSetting.categories}}"></template>  -->
<!-- 头部 -->
 <view class="typeSearchContainer"> 
    <view class="typeSearch">
     <view class="flexCenter" data-index="0" @tap="bindProductType">分类<view class="border_right"></view></view>
     <view class="flexCenter" data-index="1" @tap="bindProductType">
        <block v-if="!!typeSearch">{{typeSearch}}</block>
        <block v-else>智能排序</block>
        <view class="border_right"></view>
     </view>
     <view class="flexCenter " data-index="2" @tap="bindProductType">
        <block v-if="!!s_price.startPrice || !!s_price.endPrice">
          <text class="smoprice">￥ {{s_price.startPrice}} - {{s_price.endPrice}}</text>
        </block>
        <block v-else>价格不限</block>
        <view class="border_right"></view>
     </view>
     <view class="flexCenter" @tap="bindProductshowWay">
        <block v-if="ProductshowWay == 1">
          <image src="https://image1.sansancloud.com/xianhua/2020_3/14/17/22/17_984.jpg?x-oss-process=style/preview_120"></image>
        </block>
        <block v-else>
          <image src="https://image1.sansancloud.com/xianhua/2020_3/14/17/22/17_962.jpg?x-oss-process=style/preview_120"></image>
        </block>
     </view>
    </view>

    <view class="typeSearch_view_height"></view>
  </view>

<!-- 分类 -->
  <view class="typeSearch_item_container" v-if="showType && show0">

     <view class="typeSearch_item_left_container">
        <view v-for="(type, index) in setting.platformSetting.categories" :key="index" :data-type="type" @tap="bindTypeItem" :class="(type.active?'active':'')">{{type.name}}</view>
     </view>
     <view class="typeSearch_item_right_container">
        <view :data-categoryId="focusTypeItem.id" @tap="searchProduct">全部</view>
        <view v-for="(child, index) in focusTypeItem.children" :key="index" :data-categoryId="child.id" @tap="searchProduct">{{child.name}}</view>
     </view>
  </view>

  <!-- 排序 -->
  <view class="typeSearch_item_container" v-if="showType && show1">
    <view class="typeSearch_Sort_container">
      <view data-orderType="0" @tap="searchProduct">默认排序</view>
      <view data-orderType="102" @tap="searchProduct">价格升序</view>
      <view data-orderType="2" @tap="searchProduct">价格降序</view>
      <view data-orderType="104" @tap="searchProduct">上架日期升</view>
      <view data-orderType="4" @tap="searchProduct">上架日期降</view>
      <view data-orderType="101" @tap="searchProduct">销量升</view>
      <view data-orderType="1" @tap="searchProduct">销量降</view>
    </view>
  </view>

    <!-- 价格排序 -->
  <view class="typeSearch_item_container" v-if="showType && show2">
    <view class="typeSearch_price_container">
      <view>
        <text>起始价</text>
        <input type="number" :style="'color:' + setting.platformSetting.defaultColor" @input="getStartValue" :value="s_price.startPrice" placeholder="不限制"></input>
      </view>
       <view>
        <text>最高价</text>
        <input type="number" :style="'color:' + setting.platformSetting.defaultColor" @input="getEndValue" :value="s_price.endPrice" placeholder="不限制"></input>
      </view>
      <button @tap="searchProductbyPrice" :style="'background:' + setting.platformSetting.defaultColor" hover-class="none" type="primary" size="default" :loading="loading"> 查询 </button>
    </view>
  </view>
  <view class="zhezhao" v-if="showType" @tap="closeZhezhao"></view>
  


    <block v-if="ProductshowWay == 2">
        <view class="product_page_container">
          <block v-if="!! productListData">
            <view class="product_items" :data-info="result" :data-link="'product_detail.html?id=' + result.id" @tap="linkUrl" v-for="(result, index) in productListData" :key="index">
              <image mode="aspectFit" :style="'height:' + sysWidth*44/100 + 'px;width:100%;'" :src="result.imagePath + '?x-oss-process=style/preview'"></image>
              <view>{{result.name}}</view>
              
              <view>
                <text class="price">￥{{result.price}}</text>
                <block v-if="result.price < result.tagPrice">
                  <text class="priceTagLine">￥{{result.tagPrice}}</text>
                </block>
              </view>
            </view>
          </block>
          <block v-else>
            <view class="noproduct">
              <image mode="widthFix" src="https://image1.sansancloud.com/xianhua/2019_12/17/11/9/46_337.jpg?x-oss-process=style/preview_120"></image>
              <text>没有相关商品</text>
            </view>
          </block>
        </view>
    </block>


    <block v-else>
     <view class="product_page_container2">
        <block v-if="!! productListData">
          <!-- <view class='product_items2' data-info='{{result}}' bindtap='toProductDetail' wx:for="{{productListData}}" wx:for-item="result" wx:key="{{result.id}}">
            <image mode="aspectFill" src='{{result.imagePath}}?x-oss-process=style/preview'></image>
            <view class='product_items2_view1'>
              
              <view>
                <text>{{result.name}}</text>
                <text class='price'>￥{{result.price}}</text>
                  
              </view>
              <text class='font2'>销量： {{result.saleCount}}</text>
            </view>
            
          </view> -->
          <view class="product_items2" :data-info="result" :data-link="'product_detail.html?id=' + result.id" @tap="linkUrl" v-for="(result, index) in productListData" :key="index">
            <image mode="aspectFill" :src="result.imagePath + '?x-oss-process=style/preview'"></image>
            <view class="product_items2_view1">
              <text class="product_name">{{result.name}}</text>
              <text class="location_name" v-if="result.longitude>0">{{result.location}}</text>
              <text class="location_name" v-else>{{result.description}}</text>
              <scroll-view class="tags_content" scroll-x>
                <text class="tags_item" v-if="result.tagArray" v-for="(tags, index2) in result.tagArray" :key="index2">{{tags}}</text>
              </scroll-view>
              <view class="product_price">
                <!-- ￥{{result.price}}
                <text wx:if='{{result.unitShow}}' style='color:#bbb;font-size:24rpx;margin-left:4rpx;letter-spacing:4rpx;'>/{{result.unitShow}}</text> -->
                <text class="price" v-if="result.promotion>0">￥{{result.promotionPrice}}</text>
                <text class="price" v-else>￥{{result.price}}</text>
                <text class="price" v-if="result.maxPrice>result.price">-{{result.maxPrice}}</text>
                <text v-if="result.unitShow" class="unitShow">/{{result.unitShow}}</text>
                <text v-if="result.maxPrice<=result.price" class="tag_price">￥{{result.tagPrice}}</text>
              </view>
            </view>
            <view class="to_car" @click.stop="readyAddCar(result)">
              <text>加入购物车</text>
            </view>
          </view>
        </block>

        <block v-else>
          <view class="noproduct">
            <image mode="widthFix" src="https://image1.sansancloud.com/xianhua/2019_12/17/11/9/46_337.jpg?x-oss-process=style/preview_120"></image>
            <text>没有相关商品</text>
          </view>
        </block>
      </view>
    </block>
  <measure-card-component v-if='showCount' :showCount="showCount" :productData="productData" :byNowParams="byNowParams" :type="bindway" @closeCard="getCloseCardFun"></measure-card-component>
<!-- <component is="measure_card2" :setting="showCount" :focusData="productData.productInfo" :measures="productData.measures" :byNowParams="byNowParams" :measurementJson="measurementJson" :type="bindway"></component> -->
</view>
</view>
</template>

<script>

export default {
  data() {
    return {
      setting: null,
      // setting   
      productListData: [],
      // 商品数据 
      sysWidth: 320,
      //图片大小
      searchProductName: "",

      /* 显示或影藏 */
      showType: false,
      show0: false,
      show1: false,
      show2: false,
      // topName: {
      //   SearchProductName: "",//头部搜索的
      // },
      focusTypeItem: null,
      bindProductTypeIndex: null,
      ProductshowWay: 1,
      // ProductshowWay列表显示方法 
      typeSearch: '',
      // typeSearch的字体 
      s_price: {
        // 查询的价格 
        startPrice: "",
        endPrice: ""
      },
      productData: {},
      measurementJson: null,
      byNowParams: {
        productId: '',
        itemCount: 1,
        shopId: '',
        cartesianId: '0',
        chatOrder: '',
        fromSource: '',
        orderType: ''
      },
      MeasureParams: [],
      showCount: false,
      bindway: "",
      reqSearch: false,

      /* 全部参数 */
      params: {
        categoryId: "",
        platformNo: "",
        belongShop: "",
        typeBelongShop: "",
        page: 1,
        showType: "",
        showColumn: "",
        productName: "",
        startPrice: "",
        endPrice: "",
        orderType: "",
        saleTypeId: "",
        promotionId: "",
        shopProductType: "",
        attrKeyValues: '',
        endPrice: '',
        startPrice: '',
        attrKeyValues: "",
        itemSpecialSaleType: '',
        tag: ""
      },
      listPage: {
        page: 1,
        pageSize: 0,
        totalSize: 0,
        curpage: 1
      },
      haveMeasuresState: false,
      selectTypeData: "",
      countGood: "",
      showGuigeType: false
    };
  },

  components: {},
  props: {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("===options===", options);

    if (options.productName) {
      this.setData({
        searchProductName: options.productName
      });
    }

    if (!!options.productTypeId) {
      options.categoryId = options.productTypeId;
    }

    if (!!options.forceSearch && options.forceSearch == 2) {
      this.setData({
        ProductshowWay: 2
      });
    } else {
      this.setData({
        ProductshowWay: 1
      });
    }

    for (let i in options) {
      for (let j in this.params) {
        if (i.toLowerCase() == j.toLowerCase()) {
          this.params[j] = options[i];
        }
      }
    }

    console.log(this.params);
    this.getData(this.params, 2);
    this.setData({
      sysWidth: getApp().globalData.sysWidth,
      setting: getApp().globalData.setting,
      reqSearch: false
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.setData({
      setting: getApp().globalData.setting
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;

    if (that.reqSearch) {
      //搜索
      console.log("从搜索页面返回");
      var pages = getCurrentPages();
      var currPage = pages[pages.length - 1]; //当前页面

      console.log(currPage); //就可以看到data里mydata的值了

      console.log("搜索名称", that.searchValue);
      that.setData({
        searchProductName: that.searchValue
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.listPage.page = 1;
    this.params.page = 1;
    this.getData(this.params, 2);
    wx.showNavigationBarLoading();
    wx.hideNavigationBarLoading(); //完成停止加载

    wx.stopPullDownRefresh(); //停止下拉刷新
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;

    if (that.listPage.totalSize > that.listPage.curPage * that.listPage.pageSize) {
      that.listPage.page++;
      that.params.page++;
      this.getData(this.params);
    }
  },
  methods: {
    getCloseCardFun:function(data){
        let that=this;
        console.log("===getCloseCardFun==",data)
        that.setData({showCount:false})
    },
    /* 点击分类 */
    bindProductType: function (e) {
      var index = e.currentTarget.dataset.index;

      if (index == this.bindProductTypeIndex) {
        this.showType = false;
        this.setData({
          showType: this.showType,
          bindProductTypeIndex: null
        });
      } else {
        this.showType = true;
        this.bindProductTypeIndex = index;

        if (index == 0) {
          this.show0 = true;
          this.show1 = false;
          this.show2 = false;
        } else if (index == 1) {
          this.show0 = false;
          this.show1 = true;
          this.show2 = false;
        } else if (index == 2) {
          this.show0 = false;
          this.show1 = false;
          this.show2 = true;
        }

        this.setData({
          show0: this.show0,
          show1: this.show1,
          show2: this.show2,
          showType: this.showType,
          bindProductTypeIndex: this.bindProductTypeIndex
        });
      }
    },

    /* 点击遮罩层 */
    closeZhezhao: function () {
      this.showType = false;
      this.setData({
        showType: false,
        bindProductTypeIndex: null
      });
    },

    /* 点击分类大项 */
    bindTypeItem: function (event) {
      console.log(event.currentTarget.dataset.type);

      for (let i = 0; i < this.setting.platformSetting.categories.length; i++) {
        if (this.setting.platformSetting.categories[i].id == event.currentTarget.dataset.type.id) {
          this.setting.platformSetting.categories[i].active = true;
        } else {
          this.setting.platformSetting.categories[i].active = false;
        }
      }

      this.setData({
        setting: this.setting
      });
      this.listPage.page = 1;
      this.params.page = 1;

      if (event.currentTarget.dataset.type.id == "all") {
        this.params.categoryId = '';
        this.getData(this.params, 2);
        this.setData({
          showType: false,
          bindProductTypeIndex: null
        });
        var allItem = {
          id: ""
        };
        this.setData({
          focusTypeItem: allItem
        });
      } else {
        this.setData({
          focusTypeItem: event.currentTarget.dataset.type
        });
        var focus = event.currentTarget.dataset.type;

        if (focus.children.length == 0) {
          this.params.categoryId = focus.id;
          this.getData(this.params, 2);
          this.setData({
            showType: false,
            bindProductTypeIndex: null
          });
        }
      }
    },
    ChangeParam: function (params) {
      var returnParam = "";

      for (let i in params) {
        returnParam += "&" + i + "=" + params[i];
      }

      console.log(returnParam);
      return returnParam;
    },

    /* 获取数据 */
    getData: function (param, ifAdd) {
      //根据把param变成&a=1&b=2的模式
      if (!ifAdd) {
        ifAdd = 1;
      } //var postParam = this.ChangeParam(param)
      //param.page = this.listPage.page


      var customIndex = getApp().globalData.AddClientUrl("/more_product_list.html", param, 'get', '1'); // wx.showLoading({
      //   title: 'loading'
      // })

      getApp().globalData.showToastLoading('loading', true);
      var that = this;
      wx.request({
        url: customIndex.url,
        header: getApp().globalData.header,
        success: function (res) {
          console.log(res.data);
          let data = res.data.result;
          that.listPage.pageSize = res.data.pageSize;
          that.listPage.curPage = res.data.curPage;
          that.listPage.totalSize = res.data.totalSize;
          let dataArr = that.productListData;
          let tagArray = [];

          for (let i = 0; i < data.length; i++) {
            if (data[i].tags && data[i].tags != '') {
              tagArray = data[i].tags.slice(1, -1).split("][");
              data[i].tagArray = tagArray;
            }
          }

          console.log("data", data);

          if (ifAdd == 2) {
            dataArr = [];
          }

          if (!data || data.length == 0) {
            that.setData({
              productListData: null
            });
          } else {
            if (dataArr == null) {
              dataArr = [];
            }

            dataArr = dataArr.concat(data);
            that.setData({
              productListData: dataArr
            });
          }

          wx.hideLoading();
        },
        fail: function (res) {
          console.log("fail");
          wx.hideLoading();
          getApp().globalData.loadFail();
        }
      });
    },
    linkUrl: function (e) {
      console.log("===e====", e);
      let linkUrl = e.currentTarget.dataset.link;

      if (linkUrl == "search_page.html") {
        this.setData({
          reqSearch: true
        });
      }

      getApp().globalData.linkEvent(linkUrl);
    },

    /* 查找商品 */
    getSearchProductName: function (e) {
      var that = this;

      if (e.detail.value) {
        that.params.productName = e.detail.value;
      } else {
        that.params.productName = '';
        that.setData({
          searchProductName: that.params.productName
        });
      }

      var customIndex = that.more_product_list_URLFun(that.params);
      console.log(customIndex); // wx.showLoading({
      //   title: 'loading'
      // })

      getApp().globalData.showToastLoading('loading', true);
      wx.request({
        url: customIndex.url,
        header: getApp().globalData.header,
        success: function (res) {
          console.log(res.data);
          wx.hideLoading();

          if (!res.data.result || res.data.result.length == 0) {
            that.setData({
              productListData: null
            });
          } else {
            that.setData({
              productListData: res.data.result
            });
          }
        },
        fail: function () {
          wx.hideLoading();
          getApp().globalData.loadFail();
        }
      });
    },

    /* 分类查询 */
    searchProduct: function (event) {
      var that = this;
      this.setData({
        showType: false,
        bindProductTypeIndex: null
      });
      console.log(event.currentTarget.dataset);
      var focusKey = event.currentTarget.dataset;
      console.log(this.params);

      for (let i in focusKey) {
        for (let j in this.params) {
          if (i.toLowerCase() == j.toLowerCase()) {
            this.params[j] = focusKey[i];
          }
        }
      }

      switch (this.params.orderType) {
        case '0':
          {
            this.setData({
              typeSearch: '默认排序'
            });
            break;
          }
          ;

        case '102':
          {
            this.setData({
              typeSearch: '价格升序'
            });
            break;
          }
          ;

        case '2':
          {
            this.setData({
              typeSearch: '价格降序'
            });
            break;
          }
          ;

        case '104':
          {
            this.setData({
              typeSearch: '上架日期升'
            });
            break;
          }
          ;

        case '4':
          {
            this.setData({
              typeSearch: '上架日期降'
            });
            break;
          }
          ;

        case '101':
          {
            this.setData({
              typeSearch: '销量升'
            });
            break;
          }
          ;

        case '1':
          {
            this.setData({
              typeSearch: '销量降'
            });
            break;
          }
          ;
      }

      console.log(this.params);
      this.params.page = 1;
      var customIndex = this.more_product_list_URLFun(this.params);
      console.log(customIndex); // wx.showLoading({
      //   title: 'loading'
      // })

      getApp().globalData.showToastLoading('loading', true);
      that.listPage.page = 1;
      that.params.page = 1;
      wx.request({
        url: customIndex.url,
        header: getApp().globalData.header,
        success: function (res) {
          that.listPage.pageSize = res.data.pageSize;
          that.listPage.curPage = res.data.curPage;
          that.listPage.totalSize = res.data.totalSize;
          console.log(res.data);
          wx.hideLoading();

          if (!res.data.result || res.data.result.length == 0) {
            that.setData({
              productListData: null
            });
          } else {
            let dataArr = [];
            dataArr = dataArr.concat(res.data.result);
            that.setData({
              productListData: dataArr
            });
          }
          /* if (!res.data.result || res.data.result.length == 0) {
            that.setData({ productListData: null })
          } else {
            that.setData({ productListData: res.data.result })
          } */

        },
        fail: function () {
          wx.hideLoading();
          getApp().globalData.loadFail();
        }
      });
    },
    more_product_list_URLFun: function (params) {
      let resule = getApp().globalData.AddClientUrl("/more_product_list.html", params);
      return resule;
    },

    /* 价格排序 */
    getStartValue: function (e) {
      this.s_price.startPrice = e.detail.value;
    },
    getEndValue: function (e) {
      this.s_price.endPrice = e.detail.value;
    },
    searchProductbyPrice: function () {
      var that = this;
      this.setData({
        showType: false,
        bindProductTypeIndex: null
      });
      var focusKey = this.s_price;
      console.log(this.params);

      for (let i in focusKey) {
        for (let j in this.params) {
          if (i.toLowerCase() == j.toLowerCase()) {
            this.params[j] = focusKey[i];
          }
        }
      }

      console.log(this.params);
      var customIndex = this.more_product_list_URLFun(this.params);
      console.log(customIndex);
      wx.request({
        url: customIndex.url,
        header: getApp().globalData.header,
        success: function (res) {
          console.log(res.data);

          if (!res.data.result || res.data.result.length == 0) {
            that.setData({
              productListData: null
            });
          } else {
            that.setData({
              productListData: res.data.result
            });
          }

          that.setData({
            s_price: that.s_price
          });
        }
      });
    },

    /* 商品显示方法 */
    bindProductshowWay: function () {
      if (this.ProductshowWay == 1) {
        this.setData({
          ProductshowWay: 2
        });
      } else {
        this.setData({
          ProductshowWay: 1
        });
      }
    },
    toProductDetail: function (event) {
      console.log("--------toProductDetail------");
      console.log(event.currentTarget.dataset.info);
      var info = event.currentTarget.dataset.info;
      let link = 'productDetail.html ? id = ' + info.id + "&addShopId=" + info.belongShopId;
      getApp().globalData.linkEvent(link); // wx.navigateTo({
      //   url: '../productDetail/index?id=' + info.id + "&addShopId=" + info.belongShopId,
      // })
    },

    /* 立即购买 */
    buyNow: function (e) {
      console.log("==buyNow==", e);

      if (!getApp().globalData.checkShopOpenTime()) {
        return;
      }

      if (!getApp().globalData.checkIfLogin()) {
        return;
      }

      let bindway;

      if (e.currentTarget.dataset.way) {
        bindway = e.currentTarget.dataset.way;
      } else {
        bindway = this.bindway;
      }

      console.log('=======bindway======', bindway);
      this.setData({
        haveMeasuresState: true
      });
      this.setData({
        selectTypeData: this.selectTypeData
      });
      console.log('-----------addtocart----------');
      this.addtocart();
    },
    // 这里是一个自定义方法
    readyAddCar: function (product) {
      let that = this;
      console.log("====readyAddCar=====", product);
      console.log("====byNowParams=====", that.byNowParams);
      let productInfo = product;
      that.productData.productInfo = productInfo;
      that.setData({
        productData: that.productData
      });
      console.log("====productData=====", that.productData);
      console.log("====productInfo.id=====", productInfo.id);
      that.get_product_measureFun(productInfo.id);
    },
    addtocart: function () {
      console.log("===addtocart====");

      if (!getApp().globalData.checkIfLogin()) {
        return;
      }

      var params = {
        cartesianId: '',
        productId: '',
        shopId: '',
        count: '',
        type: ''
      };

      if (this.productData.measures.length == 0) {
        params.cartesianId = '0';
      } else {
        params.cartesianId = this.measurementJson.id;
      }

      params.productId = this.productData.productInfo.id;
      params.shopId = this.productData.productInfo.belongShopId;
      params.count = this.byNowParams.itemCount;
      params.type = 'add';
      console.log('===postParams=====', params);
      this.postParams(params);
    },
    postParams: function (data) {
      console.log("==postParams====");
      var that = this;
      var customIndex = getApp().globalData.AddClientUrl("/change_shopping_car_item.html", data, 'post');
      wx.request({
        url: customIndex.url,
        data: customIndex.params,
        header: getApp().globalData.headerPost,
        method: 'POST',
        success: function (res) {
          console.log('---------------change_shopping_car_item-----------------');
          console.log(res.data);
          wx.hideLoading();

          if (that.bindway == 'cart') {
            that.setData({
              showCount: false
            });
          }

          if (data.productId == 0) {
            console.log('购物车里面的商品数量');
            that.setData({
              countGood: res.data.totalCarItemCount
            });
          } else {
            if (res.data.productId && res.data.productId != 0) {
              if (data.count == 0) {
                console.log('通过加入购物车来确定购物车里面的商品数量');
              } else {
                wx.showToast({
                  title: '加入购物车成功'
                });
              }

              if (!!res.data.totalCarItemCount || res.data.totalCarItemCount == 0) {
                that.setData({
                  countGood: res.data.totalCarItemCount
                });
              }
            } else {
              wx.showToast({
                title: res.data.errMsg,
                image: '/wxcomponents/images/icons/tip.png',
                duration: 2000
              });
            }
          }
        },
        fail: function (res) {
          wx.hideLoading();
          getApp().globalData.loadFail();
        }
      });
    },

    /* 
       规格操作
    */
    //提交规格产品
    submitMeasure: function (id) {
      var that = this;
      let focusProduct = this.MeasureItem;
      let measurementJson = this.measurementJson;
      let data = {};
      data.cartesianId = measurementJson.id;
      data.productId = focusProduct.id;
      data.shopId = focusProduct.belongShopId;
      data.count = 1;
      data.type = 'add';
      var customIndex = getApp().globalData.AddClientUrl("/change_shopping_car_item.html", data, 'post');
      wx.request({
        url: customIndex.url,
        data: customIndex.params,
        header: getApp().globalData.headerPost,
        method: 'POST',
        success: function (res) {
          console.log('--------add----------');
          console.log(res.data);
          that.setData({
            showGuigeType: false
          });
        },
        fail: function (res) {
          getApp().globalData.loadFail();
        },
        complete: function () {
          wx.hideLoading();
        }
      });
    },
    //获取规格价格参数
    get_product_measureFun: function (id) {
      let that = this;
      let productId = id;
      let customIndex = getApp().globalData.AddClientUrl("/get_product_measures.html", {
        productId: productId
      });
      wx.request({
        url: customIndex.url,
        header: getApp().globalData.header,
        success: function (res) {
          console.log(res.data);
          let measures = res.data;
          that.productData.measures = measures;
          that.setData({
            productData: that.productData
          });

          if (measures.length == 0) {
            that.addtocart();
          } else {
            that.setData({
              bindway: "cart"
            });
            let info = that.productData.productInfo;
            that.byNowParams.productId = info.id;
            that.byNowParams.shopId = info.belongShopId;
            that.byNowParams.orderType = 0;
            that.setData({
              byNowParams: that.byNowParams,
              showCount: true
            });
            console.log("==byNowParams===", that.byNowParams);
            // that.chooseMeasureItem();
          }
        },
        fail: function (res) {
          console.log("fail");
          getApp().globalData.loadFail();
        },
        complete: function () {}
      });
    },
    //获取规格价格参数
    // get_measure_cartesionFun: function () {
    //   this.setData({
    //     measurementJson: {
    //       waitDataState: false
    //     }
    //   });
    //   let productId = this.productData.productInfo.id;
    //   let postStr = '';

    //   if (this.MeasureParams.length == 0) {
    //     this.byNowParams.cartesianId = '0';
    //     this.setData({
    //       measurementJson: {
    //         waitDataState: true
    //       }
    //     }); //没有规格时 不需要等待请求

    //     return;
    //   }

    //   for (let i = 0; i < this.MeasureParams.length; i++) {
    //     postStr += this.MeasureParams[i].value + ',';
    //   }

    //   let param = {};
    //   param.productId = productId;
    //   param.measureIds = postStr;
    //   console.log(postStr);
    //   let customIndex = getApp().globalData.AddClientUrl("/get_measure_cartesion.html", param);
    //   var that = this;
    //   wx.request({
    //     url: customIndex.url,
    //     header: getApp().globalData.header,
    //     success: function (res) {
    //       console.log("===measurementJson===", res.data);
    //       that.byNowParams.cartesianId = res.data.id;
    //       that.setData({
    //         measurementJson: res.data
    //       });
    //       that.measurementJson.waitDataState = true;
    //       that.setData({
    //         measurementJson: that.measurementJson
    //       });
    //       that.byNowParams.itemCount = that.measurementJson.minSaleCount;
    //       that.setData({
    //         byNowParams: that.byNowParams
    //       });
    //     },
    //     fail: function (res) {
    //       console.log("fail");
    //       getApp().globalData.loadFail();
    //     },
    //     complete: function () {}
    //   });
    // },
    // /* 初始化 选规格 */
    // chooseMeasureItem: function (event) {
    //   console.log('----------初始化规格参数-----------', event);
    //   let that = this;
    //   let productData = that.productData;
    //   let focusProduct = productData;
    //   let selectTypeData = [];

    //   for (let i = 0; i < focusProduct.measures.length; i++) {
    //     focusProduct.measures[i].checkedMeasureItem = 0; //初始化选择的数据

    //     let param = {};
    //     let selectTypeDataItem = {};
    //     param.name = focusProduct.measures[i].name;
    //     param.value = focusProduct.measures[i].productAssignMeasure[0].id;
    //     selectTypeDataItem.type = focusProduct.measures[i].name;
    //     selectTypeDataItem.value = focusProduct.measures[i].productAssignMeasure[0].measureName;
    //     console.log('=====param=====', param);
    //     that.MeasureParams.push(param);
    //     selectTypeData.push(selectTypeDataItem);
    //   }

    //   that.selectTypeData = selectTypeData;
    //   console.log('====that.data.selectTypeData======', this.selectTypeData);
    //   that.setData({
    //     productData: focusProduct
    //   });
    //   console.log('===MeasureParams====', this.MeasureParams);
    //   that.get_measure_cartesionFun();
    // },
    //选择规格小巷的---显示
    // radioChange: function (e) {
    //   let that = this;
    //   let flag = false;
    //   console.log("====radioChange=====", e);
    //   console.log('====that.data.selectTypeData======', that.selectTypeData);

    //   if (that.selectTypeData) {
    //     console.log('1111111');

    //     for (let i = 0; i < that.selectTypeData.length; i++) {
    //       if (e.currentTarget.dataset.type == that.selectTypeData[i].type) {
    //         that.selectTypeData.splice(i, 1, e.currentTarget.dataset);
    //         flag = true;
    //       }
    //     }

    //     if (!flag) {
    //       that.selectTypeData.splice(that.selectTypeData.length, 1, e.currentTarget.dataset);
    //       flag = false;
    //     }
    //   } else {
    //     console.log('222222');
    //     that.selectTypeData = [];
    //     that.selectTypeData.splice(0, 1, e.currentTarget.dataset);
    //     that.setData({
    //       selectTypeData: that.selectTypeData
    //     });
    //   }

    //   console.log('====that.data.selectTypeData======', that.selectTypeData);
    //   let index = e.currentTarget.dataset.index;
    //   let indexJson = getApp().globalData.getSpaceStr(index, '-'); //console.log(indexJson)

    //   let focusItem = that.productData;
    //   focusItem.measures[indexJson.str1].checkedMeasureItem = indexJson.str2;
    //   that.setData({
    //     productData: focusItem
    //   });
    // },
    //选择规格小巷---获取数据
    // chooseMeasure: function (e) {
    //   console.log(e.detail.value);
    //   let chooseMeasureJson = getApp().globalData.getSpaceStr(e.detail.value, '-');
    //   console.log(chooseMeasureJson);

    //   for (let i = 0; i < this.MeasureParams.length; i++) {
    //     if (this.MeasureParams[i].name == chooseMeasureJson.str1) {
    //       this.MeasureParams[i].value = chooseMeasureJson.str2;
    //     }
    //   }

    //   this.get_measure_cartesionFun();
    // },
    closeGuigeZhezhao: function () {
      this.setData({
        showGuigeType: false
      });
      this.MeasureParams = [];
    },
    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  }
};
</script>
<style>
@import "./index.css";
</style>